#ifndef __LED__H
#define __LED__H

#include "typedef.h"
#include "SwTimer.h"
#include "Queue.h"

enum
{
	SHINE_TIME_ID
	, LAMP_TEST_TIMER_ID
};

typedef enum _LedFunction
{
	 LEDFUN_OFF = 0
	,LEDFUN_SHINE_MIX = 0x01
	,LEDFUN_SHINE_MAX = 0xFA
	,LEDFUN_LAMP_TEST = 0xFB
	,LEDFUN_LOCAL_CTRL = 0xFC
	,LEDFUN_ON	= 0xFF
}LedFunction;

typedef enum _LedWorkMode
{
	 OP_INIT = 0
	,OP_OFF 
	,OP_ON	
	,OP_SHINE
}LedWorkMode;

enum
{
	 LED_M0 = 0
	,LED_M1
	,LED_M2
	,LED_M3
	,LED_M4
};


typedef enum _LedColor
{
	 COLOR_BLUE = 1
	,COLOR_RED
	,COLOR_GREEN
	,COLOR_AMBER
	,COLOR_ORANGE
	,COLOR_WHITE
}LedColor;


typedef enum _LedMODE
{
	 LOCAL_CONTROL = 0
	,OVERRIDE_CONTROL
	,LAMPTEST_CONTROL
}LedMode;

typedef struct _tagLedCmd
{
	uint16		m_Off;	
	uint16 		m_On;		
	uint8 		m_Color;		
	LedWorkMode	m_bWorkMode;	
}LedCmd;

typedef struct _LedCtrlState
{
#ifdef BIG_EDIAN
	uint8			m_Reserved:5;
	uint8			m_isLampTestEnabled:1;
	uint8			m_isOverrideStateEnabled:1;
	uint8			m_hasALocalCtrlState:1;
#else
	uint8			m_hasALocalCtrlState:1;
	uint8			m_isOverrideStateEnabled:1;
	uint8			m_isLampTestEnabled:1;
	uint8			m_Reserved:5;
#endif
}LedCtrlState;

typedef struct _LedColorCapabilities
{
#ifdef BIG_EDIAN
	uint8 m_Reserved1:1;	//BIT[7]
	uint8 m_White:1;		//BIT[6]
	uint8 m_Orange:1;		//BIT[5]
	uint8 m_Amber:1;		//BIT[4]
	uint8 m_Green:1;		//BIT[3]
	uint8 m_Red:1;			//BIT[2]
	uint8 m_Blue:1;			//BIT[1]
	uint8 m_Reserved0:1;	//BIT[0]
#else
	uint8 m_Reserved0:1;	//BIT[0]
	uint8 m_Blue:1;			//BIT[1]
	uint8 m_Red:1;			//BIT[2]
	uint8 m_Green:1;		//BIT[3]
	uint8 m_Amber:1;		//BIT[4]
	uint8 m_Orange:1;		//BIT[5]
	uint8 m_White:1;		//BIT[6]
	uint8 m_Reserved1:1;	//BIT[7]
#endif
}LedColorCapabilities;

typedef	int (*SetLedFun)(void* pLed, uint8 color, Bool isOn);
#define MAX_CMD_COUNT 4
#define LED_TIME 	100

typedef struct _tagLedItem
{
	uint8 			m_LedId;
	uint8			m_DefaultColorInLocalCtrlState: 4;
	uint8			m_DefaultColorInOverrideCtrlState: 4;
	uint8			m_ColorCapabilities;
}LedItem;

typedef struct _tagLed
{
	uint8 			m_LedId;
	
	uint8			m_ColorInOverrideCtrlState: 4;
	uint8			m_ColorInLocalCtrlState: 4;

	uint8			m_DefaultColorInLocalCtrlState: 4;
	uint8			m_DefaultColorInOverrideCtrlState: 4;
	
	LedColorCapabilities	m_ColorCapabilities;
	uint8			m_bState;

	LedCtrlState	m_LedStates;
	
	uint16			m_LampTestDuration;
	
	uint8			m_ColorInLampTest: 4;
	uint8			m_Reserved: 4;

	LedCmd			m_LedCmd;
	uint32 			m_Ticks;
	
	SwTimer			m_Timer;
	TimerManager	*m_pTimerManager;

	SetLedFun 		SetLed;

	Queue			m_Queue;
	LedCmd			m_QueueBuff[MAX_CMD_COUNT];
}Led;

void Led_Init(Led* pLed, TimerManager* pTm, const LedItem* pLedItem, SetLedFun setLed);
void Led_SetWorkMode(Led* pLed, LedWorkMode mode, uint16 shineOff, uint16 ShineOn, uint8 Control);
void Led_SetWorkModeEx(Led* pLed, LedWorkMode mode, uint16 shineOff, uint16 ShineOn, uint8 Control, uint8 color);
void Led_Release(Led* pLed);
Bool Led_LampTest(Led* pLed, uint8 color, uint16 ms);
int Led_GetState(Led* pLed, uint8* buff, uint8 len);
Bool Led_SetState(Led* pLed, uint8 color, uint8 function, uint8 onDuraton);
#endif

